﻿using System;
using System.Data;

namespace mklib.json
{
	/// <summary>
	/// C#类型与JSType类型转换
	/// </summary>
	internal static class JsonTypeConvertor
	{
		/// <summary>
		/// 通过TypeCode转换
		/// </summary>
		/// <returns>JSType</returns>
		/// <param name="tc">TypeCode</param>
		internal static JsonValueType GetJSType(TypeCode tc)
		{
			switch (tc) {
			case TypeCode.Boolean:
				return JsonValueType.Boolean;
			case TypeCode.Byte:
			case TypeCode.SByte:
			case TypeCode.Int16:
			case TypeCode.Int32:
			case TypeCode.Int64:
			case TypeCode.UInt16:
			case TypeCode.UInt32:
			case TypeCode.UInt64:
				return JsonValueType.Int;
			case TypeCode.Char:
			case TypeCode.String:
				return JsonValueType.String;
			case TypeCode.DateTime:
				return JsonValueType.Int;
			case TypeCode.DBNull:
			case TypeCode.Empty:
				return JsonValueType.Null;
			case TypeCode.Decimal:
			case TypeCode.Double:
			case TypeCode.Single:
				return JsonValueType.Float;
			case TypeCode.Object:
				return JsonValueType.Object;
			default:
				throw new NotSupportedException (String.Format ("{0}", tc));
			}
		}

		/// <summary>
		/// 通过类型转换
		/// </summary>
		/// <returns>The JS type.</returns>
		/// <param name="t">T.</param>
		internal static JsonValueType GetJSType(Type t)
		{
			return GetJSType (Type.GetTypeCode (t));
		}

		/// <summary>
		/// 直接获取
		/// </summary>
		/// <returns>The JS type.</returns>
		/// <param name="o">O.</param>
		internal static JsonValueType GetJSType(object o)
		{
			return o == null ? JsonValueType.Null : GetJSType (o.GetType ());
		}


	}
}